import 'dart:async';
import '../function.dart';

int decimal=3;
class PriceModel{
  String name;
  String type;
  String invite;
  String current;
  String max;
  String min;
  String change;
  String amount;
  bool upDown=true;
  PriceModel(this.name,this.type,this.invite,{
    this.current,
    this.max,
    this.min,
    this.change
  });
  Future<Null> getPrice()async{
    Map data;
    switch(this.type){
      case "wkj":data=await getWkjPrice();break;
      case "uyulin":data=await getUyulinPrice();break;
      case "fatbtc":data=await getFatbtcPrice();break;
      case "zxx":data=await getZxxPrice();break;
      case "playwkc":data=await getPlayWkcPrice();break;
      case "6lk":data=await get6lkPrice();break;
      case "66otc":data=await get66otcPrice();break;
      case "firefox":data=await getFirefoxPrice();break;
      case "51jd":data=await get51jdPrice();break;
    }
    this.current=data['current'].toString();
    this.max=data['max'].toString();
    this.min=data['min'].toString();
    this.upDown=data['upDown'];
    this.amount=data['amount'].toString();
    this.change=data['change'];
  }
}
List<PriceModel> platforms=[
  new PriceModel("玩客家", "wkj", "http://www.wkj.link/?invit=XQRBZN"),
  //new PriceModel("悠雨林","uyulin","http://beenl.pro"),
  new PriceModel("胖比特","fatbtc","http://www.fatbtc.us/m/reg/u101117855"),
  new PriceModel("Z网","zxx" , "http://zxx.cm/login/register/invit/QEWGVN.html"),
  new PriceModel("PlayWKC", "playwkc", "https://www.playwkc.com/register?ref=DoznqZ0xyb"),
  new PriceModel("6lk.com", "6lk", "https://www.6lk.com/register?ref=5dK0nWMVjr"),
  new PriceModel("66OTC", "66otc", "https://www.66otc.com/register?ref=VpakGXVVaP"),
  new PriceModel("火狐狸", "firefox", "https://firefoxotc.com/trade?referral_code=PmcPUQxXCFUK"),
  new PriceModel("蓝枫林", "51jd", "http://www.51jd.ink/?invit=RJ924677"),
];
Future<Map> getWkjPrice() async {
  var url="http://www.wkj.link/Ajax/getJsonTop?market=wkb_cny";
  var res=await get(url, null, null);
  var data=res['info'];
  Map price={
    "current":data["new_price"].toStringAsFixed(decimal),
    "upDown":data["change"]<0?false:true,
    "max":data['max_price'].toStringAsFixed(decimal),
    "min":data['min_price'].toStringAsFixed(decimal),
    "amount":(data['volume']/10000).toStringAsFixed(decimal)+"万",
    "change":data['change'].toStringAsFixed(decimal),
  };
  return price;

}
Future<Map> getUyulinPrice() async {
  var url = "http://beenlapi.moduointerface.net/public/allMarketTradeData";
  var header = {
    "X-Custom-UserAgent":"vbourse/v1.1.0",
    "Cache-Control":"no-cache",
    "Origin":"http://beenl.pro",
    "Referer":"http://beenl.pro/"
  };
  var res=await post(url: url,data: {"sortFlag":"false"},header: header,contentType: "application/x-www-form-urlencoded");
  var data=res['body'][1];
  Map price={
    "current":data["closePrice"].toStringAsFixed(decimal),
    "upDown":data["mchange"]<0?false:true,
    "max":data['maxPrice'].toStringAsFixed(decimal),
    "min":data['minPrice'].toStringAsFixed(decimal),
    "amount":(data['volume']/10000).toStringAsFixed(decimal)+"万",
    "change":data['mchange'].toStringAsFixed(decimal),
  };
  return price;
}
Future<Map> getFatbtcPrice() async {
  var time=new DateTime.now().millisecondsSinceEpoch;
  var url = "https://www.fatbtc.us/m/allticker/$time";
  var res=await get(url, null, null);
  var data=res["data"]["wkcfcny_ticker"];
  print(data);
  print(data["upOrDown"]);
  print(data['close']);
  print(data['open']);
  Map price={
    "current":data["close"].toStringAsFixed(decimal),
    "upDown":(data['close']-data['open'])<0?false:true,
    "max":data['high'].toStringAsFixed(decimal),
    "min":data['low'].toStringAsFixed(decimal),
    "amount":(data['amount']/10000).toStringAsFixed(decimal)+"万",
    "change":((data['close']-data['open'])/data['open']*100).toStringAsFixed(decimal),
  };
  return price;
}
Future<Map> getZxxPrice() async {
  var url = "https://www.fmtol.com/Home/Index/getZxxPrice";
  var res=await get(url, null, null);
  String current=res['current'];
  Map price={

    "current":double.parse(current.substring(1)).toStringAsFixed(decimal),
    "upDown":res["daily"]<0?false:true,
    "max":(res['high']*6.7).toStringAsFixed(decimal),
    "min":(res['low']*6.7).toStringAsFixed(decimal),
    "amount":(res['amount']/10000).toStringAsFixed(decimal)+"万",
    "change":res['daily'].toStringAsFixed(decimal),
  };
  return price;
}
Future<Map>  getPlayWkcPrice() async {
  var url = "https://www.playwkc.com/api/wkc.php";
  var res=await get(url, null, null);
  Map price={
    "current":double.parse(res['new_price']).toStringAsFixed(decimal),
    "upDown":true,
    "max":double.parse(res['max_price']).toStringAsFixed(decimal),
    "min":double.parse(res['min_price']).toStringAsFixed(decimal),
    "amount":(double.parse(res['amount'])/10000).toStringAsFixed(decimal)+"万",
    "change":"-"
  };
  return price;
}
Future<Map> get6lkPrice() async {
  var url="https://www.6lk.com/api/ticker";
  var res=await get(url, null, null);
  var data=res["ticker"];
  Map price={
    "current":double.parse(data['last']).toStringAsFixed(decimal),
    "upDown":double.parse(data["change"])<0?false:true,
    "max":double.parse(data['high']).toStringAsFixed(decimal),
    "min":double.parse(data['low']).toStringAsFixed(decimal),
    "amount":(double.parse(data['vol'])/10000).toStringAsFixed(decimal)+"万",
    "change":data['change']
  };
  return price;
}
Future<Map> get66otcPrice() async {
  var url = "https://api.66otc.com/api/ticker?symbol=WKC";
  var res=await get(url, null, null);
  var data=res["ticker"];
  Map price={
    "current":double.parse(data['last']).toStringAsFixed(decimal),
    "upDown":double.parse(data["change"])<0?false:true,
    "max":double.parse(data['high']).toStringAsFixed(decimal),
    "min":double.parse(data['low']).toStringAsFixed(decimal),
    "amount":(double.parse(data['vol'])/10000).toStringAsFixed(decimal)+"万",
    "change":data['change']
  };
  return price;
}
Future<Map> getFirefoxPrice() async {
  var header = {
    "Cache-Control":"max-age=0",
  };
  var url = "https://www.firefoxotc.com/api/exchange/ticker_list";
  var res=await post(url: url,data: {"currency":"FFC"},header: header,contentType: "application/x-www-form-urlencoded");
  var data=res['data']['tickers']['OSC_FFC'];
  Map price={
    "current":data['last'].toStringAsFixed(decimal),
    "upDown":true,
    "max":data['high'].toStringAsFixed(decimal),
    "min":data['low'].toStringAsFixed(decimal),
    "amount":(data['volume']/10000).toStringAsFixed(decimal)+"万",
    "change":"-"
  };
  return price;

}
Future<Map> get51jdPrice() async {
  var url = "https://www.51jd.ink/trade/index_json/market/lk_doge";
  var res=await get(url, null, null);
  var data=res['top'];
  Map price={
    "current":double.parse(data[0]).toStringAsFixed(decimal),
    "upDown":double.parse(data[6])<0?false:true,
    "max":double.parse(data[4]).toStringAsFixed(decimal),
    "min":double.parse(data[3]).toStringAsFixed(decimal),
    "amount":(double.parse(data[5])/10000).toStringAsFixed(decimal)+"万",
    "change":double.parse(data[6]).toStringAsFixed(decimal),
  };
  return price;
}